package com.tiennt.android.threeview;

import java.util.List;

import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class ThirdView extends MapActivity {

	MapView mMapView;
	MapController mMapController;
	MyMapOverlay mOverlay;

	LocationManager mLocMgr;

	@Override
	protected void onCreate(Bundle icicle) {
		// TODO Auto-generated method stub

		super.onCreate(icicle);
		setContentView(R.layout.activity_third_view);
		Toast.makeText(ThirdView.this, "ThirdView", Toast.LENGTH_LONG).show();
		mMapView = (MapView) findViewById(R.id.mapview);
		mMapController = mMapView.getController();
		List<Overlay> overlayList = mMapView.getOverlays();

		Drawable defaultMaker = getResources().getDrawable(
				R.drawable.ic_launcher);
		mOverlay = new MyMapOverlay(ThirdView.this, defaultMaker);
		overlayList.add(mOverlay);

		mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_COARSE);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_HIGH);

		String provider = mLocMgr.getBestProvider(criteria, true);
		mLocMgr.requestLocationUpdates(provider, 0, 0, locationListener);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		mLocMgr.removeUpdates(locationListener);
	}

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	LocationListener locationListener = new LocationListener() {

		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub

		}

		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub

		}

		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub

		}

		public void onLocationChanged(Location location) {
			// TODO Auto-generated method stub
			GeoPoint geoPoint = new GeoPoint(
					(int) (location.getLatitude() * 1E6),
					(int) (location.getLongitude() * 1E6));
			mMapController.animateTo(geoPoint);
			mMapController.setZoom(17);

			OverlayItem item = new OverlayItem(geoPoint, "Location",
					"New location");
			mOverlay.addOverlayItem(item);
			mMapView.invalidate();
		}
	};
}
